/****************************************************************************************
 * Copyright (c) 2010~2012 All Rights Reserved by
 * G-Net Integrated Service Co., Ltd. 
 ****************************************************************************************/
package com.kingtone.rongji.adapter;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.Comments;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.kingtone.rongji.ui.CustomDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * @Title: MyCommentsAdapter.java
 * @author: wenhui.li
 * @date: 2015-4-14 下午7:06:26
 */
public class MyCommentsAdapter extends BaseAdapter
{
	private Context context = null;
	private ArrayList<Comments> commentsList = null;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	public MyCommentsAdapter(Context context, ArrayList<Comments> commentsList)
	{
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.commentsList = commentsList;
	}

	@Override
	public int getCount()
	{
		return commentsList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return commentsList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view;
		// 如果已加载过，使用缓存技术不再重新加载
		if (convertView != null)
		{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		} else
		{
			view = inflater.inflate(R.layout.my_comments_item, null);
			holder = new ViewHolder();
			holder.commentsProductIcon = (ImageView) view.findViewById(R.id.comments_product_icon);
			holder.commentsProductName = (TextView) view.findViewById(R.id.comments_product_name);
			holder.commentsProducNo = (TextView) view.findViewById(R.id.comments_product_no);
			holder.commentsOrderNo = (TextView) view.findViewById(R.id.comments_order_no);
			holder.commentsByDate = (TextView) view.findViewById(R.id.comments_buy_date);
			holder.commentsByPrice = (TextView) view.findViewById(R.id.comments_buy_price);
			holder.commentsDetailBtn = (Button) view.findViewById(R.id.comments_btn_detail);
		}
		holder.commentsDetailBtn.setTag(position);	
		Comments comments = commentsList.get(position);
		view.setTag(holder);
		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
		String productIcon = AppRestClient.convertImageUrl(comments.getProductId(), comments.getImageName());
		if (!TextUtils.isEmpty(productIcon))
		{
			ImageLoader.getInstance().displayImage(productIcon, holder.commentsProductIcon);
		}
		holder.commentsProductName.setText(context.getString(R.string.comments_product_name) + comments.getProductName());
		holder.commentsProducNo.setText(context.getString(R.string.comments_product_no) + comments.getProductId());
		holder.commentsOrderNo.setText(context.getString(R.string.order_no) + comments.getOrderNumber());
		holder.commentsByDate.setText(context.getString(R.string.comments_buy_date) + RongJiUtils.revertDate(comments.getInDate()));
		holder.commentsByPrice.setText(context.getString(R.string.comments_buy_price) + comments.getUnitPrice());

		holder.commentsDetailBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(v instanceof Button)
				{
					int index = (Integer) v.getTag();
					Comments temp = commentsList.get(index);
					String title = temp.getTitle();
					int rating = temp.getRating();
					String advantage = temp.getMeritText();
					String disvantage = temp.getFaultsText();
					String description = temp.getMemo();
					Dialog dialog = CustomDialog.show((Activity) context, rating, title, advantage, disvantage, description,
							new View.OnClickListener()
					{

						@Override
						public void onClick(View v)
						{
							CustomDialog.dismiss();
						}
					});
					dialog.show();
				}
			}
		});
		return view;
	}

	private class ViewHolder
	{
		public ImageView commentsProductIcon;
		public TextView commentsProductName;
		public TextView commentsProducNo;
		public TextView commentsOrderNo;
		public TextView commentsByDate;
		public TextView commentsByPrice;
		public Button commentsDetailBtn;
	}
}
